﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Net.Sockets;
using BCGPSCommon;
using System.Net;
using AppSocketModel;

namespace BCGPS.DataRealy.Service
{
    public class UdpDataSendClient
    {
        private UdpClient udpClient;
        private IPEndPoint iPEndPoint;

        public UdpDataSendClient(RelayDto relayDto)
        {
            udpClient = new UdpClient();
            iPEndPoint = new IPEndPoint(IPAddress.Parse(relayDto.RelayUrl), relayDto.Port);
        }

        public void Dispose()
        {
            if (udpClient != null)
            {
                try
                {
                    udpClient.Close();
                    udpClient.Dispose();
                }
                catch (System.Exception ex)
                {
                    Msg.WirteLine(ex.Message);
                }
            }
        }

        #region 重置IP和端口号
        public void ResetIPEndPoint(IPAddress ipAddress, int port)
        {
            iPEndPoint = new IPEndPoint(ipAddress, port);
        }
        #endregion



        #region 发送数据
        public void UdpSendMsg(string msg)
        {
            try
            {
                byte[] sendbytes = Encoding.ASCII.GetBytes(msg);
                udpClient.Send(sendbytes, sendbytes.Length, iPEndPoint);

            }
            catch (System.Exception ex)
            {
                Msg.WirteLine(ex.Message, 20, '*');
            }
        }
        #endregion
    }
}
